# -*- coding: utf-8 -*-
'''
@文件    :role.py
@说明    :角色管理
@时间    :2023/12/19 19:57:04
@作者    :浪晋
@版本    :1.0
'''

from app.admin import auth
from flask import request,g
from utils.requetFunc import RequestParser,pageConvert
from utils.responseFunc import resultMsg
from utils.commomFunc import build_tree
from json import loads


@auth.route("/role/list",methods=["post"])
def roleList():
    """
    @功能    :角色列表
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    role = body.get("role")
    status = body.get("status")
    pageSize = body.get("pageSize")
    pageNum = body.get("pageNum")
    parse = RequestParser()
    parse.add_argument(value=role,name="角色名称",remark="")
    parse.add_argument(value=status,name="状态",remark="")
    parse.add_argument(value=pageSize,name="每页显示的数据数量",required=True,remark="",type=(int))
    parse.add_argument(value=pageNum,name="页码",required=True,remark="",type=(int))
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "select id,role,sort,alias,status,remark,createTime,dataType from tb_system_role where status != 0 "
    csql = "select count(0) total from tb_system_role where status != 0  "
    if role not in (None,""):
        sql = sql + " and role like '%{}%'".format(role)
        csql = csql + " and role like '%{}%'".format(role)
    if status:
        sql = sql + " and status = {}".format(status)
        csql = csql + " and status = {}".format(status)
    sql = sql + " order by sort limit {},{};".format(*pageConvert(pageNum,pageSize))
    result = db.fetchall(sql)
    cresult = db.fetchone(csql)
    return resultMsg(code=1,msg="获取成功",data=result,total=cresult["total"])



@auth.route("/role/create",methods=["post"])
def role_create():
    """
    @功能    :新增角色
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    role = body.get("role")
    sort = body.get("sort")
    alias = body.get("alias")
    remark = body.get("remark")
    parse = RequestParser()
    parse.add_argument(value=role,name="角色名称",required=True,remark="")
    parse.add_argument(value=sort,name="排序",required=True,remark="")
    parse.add_argument(value=alias,name="别名",required=True,remark="")
    parse.add_argument(value=remark,name="备注",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "insert into tb_system_role (role,alias,sort,remark) values (%s,%s,%s,%s);"
    db.execute(sql,(role,alias,sort,remark))
    db.commit()
    return resultMsg()



@auth.route("/role/edit",methods=["post"])
def roleEdit():
    """
    @功能    :修改角色
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    role = body.get("role")
    sort = body.get("sort")
    alias = body.get("alias")
    remark = body.get("remark")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="角色ID",required=True,remark="")
    parse.add_argument(value=role,name="角色名称",required=True,remark="")
    parse.add_argument(value=sort,name="排序",required=True,remark="")
    parse.add_argument(value=alias,name="别名",required=True,remark="")
    parse.add_argument(value=remark,name="备注",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_role set role = %s,sort = %s,alias = %s,remark = %s where id = %s;"
    db.execute(sql,(role,sort,alias,remark,rowId))
    db.commit()
    return resultMsg()


@auth.route("/role/del",methods=["post"])
def roleDel():
    """
    @功能    :删除角色
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="角色ID",required=True,remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_role set status = 0 where id = %s;"
    db.execute(sql,(rowId))
    db.commit()
    return resultMsg()



@auth.route("/role/state",methods=["post"])
def role_state():
    """
    @功能    :修改角色状态
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    status = body.get("status")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="角色ID",required=True,remark="")
    parse.add_argument(value=status,name="状态",required=True,remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_role set status = %s where id = %s and status != 0 ;"
    db.execute(sql,(status,rowId))
    db.commit()
    return resultMsg()


@auth.route("/role/permission/set",methods=["post"])
def role_set_permission():
    """
    @功能    :关联可操作的菜单
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    permissionIds = body.get("permissionIds")
    dataType = body.get("dataType",1)
    parse = RequestParser()
    parse.add_argument(value=rowId,name="角色ID",required=True,remark="必填",type=(int))
    parse.add_argument(value=permissionIds,name="权限ID",required=True,remark="必填",type=(list,tuple))
    parse.add_argument(value=dataType,name="数据权限",required=True,remark="必填",type=(int))
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "delete from tb_system_permission where roleId = %s ;"
    db.commit(sql,(rowId))
    sql = "update tb_system_role set dataType = %s where id = %s and status != 0 ;"
    db.commit(sql,(dataType,rowId))
    sql = "insert into tb_system_permission (roleId,menuId) values ({},%s);".format(rowId)
    db.executemany(sql,permissionIds)
    db.commit()
    return resultMsg()


@auth.route("/role/permission/get",methods=["post"])
def role_get_permission():
    """
    @功能    :获取已关联的菜单
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="角色ID",required=True,remark="必填",type=(int))
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "select a.menuId from tb_system_permission a join tb_system_menu b on a.menuId = b.id where a.roleId = %s;"
    result = [i["menuId"] for i in  db.fetchall(sql,(rowId))]
    return resultMsg(data=result)


@auth.route("/role/permission/all",methods=["post"])
def role_all_permission():
    """
    @功能    :获取可关联的所有菜单
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    roleId = g.userInfo.get("roleId")
    sql = "select a.* from tb_system_menu a join tb_system_permission b on a.id = b.menuId where b.roleId = %s and a.status = 1;"
    result = []
    for menu in db.fetchall(sql,(roleId)):
        menu.update(meta=loads(menu["meta"]) )
        result.append(menu)
    result = build_tree(result)
    return resultMsg(data=result)

    
